

package pt.ist.meic.pava.BigFatLockSync;

import pt.ist.meic.pava.SyncCommons.*;
import java.util.concurrent.Semaphore;



/**
 * 
 * @author Antonio Massingue
 * @author Joao Matos
 * @author Nuno Santos
 * 
 * This aspect implements the big fat lock synchronization strategy
 * 
 * A single lock guard all methods annotated with @Atomic
 * These methods will execute in mutual exclusion
 *
 */
public aspect BigFatLockSync {

	/**
	 * The lock
	 */
	private Semaphore bigFatLock = new Semaphore(1);
	
	
	
	
	/**
	 * If annotated with @Atomic
	 */
	
	pointcut calledAtomic(): 
		call(@Atomic * *.*(..)) && !cflowbelow(call(@Atomic * *.*(..)));
	
	
	/**
	 * Acquire the lock before executing
	 */
	before(): calledAtomic(){
		
		try {
			
			this.bigFatLock.acquire();
			
			//System.out.println("Lock acquired");
			
			
		} catch (InterruptedException e) {/*The thread was interrupted*/
			
			System.out.println("The thread was Interrupted!");
			
			e.printStackTrace();
			
			System.out.println("****************************");
			
		}
		
	}
	
	/**
	 * Release the lock after executing
	 */
	after(): calledAtomic()  {
		
		//System.out.println("Lock free");
		this.bigFatLock.release();
	}
			
}
